<?php

/**
 * recherche actions.
 *
 * @package    sinc
 * @subpackage recherche
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class rechercheActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->filters = new sfGuardGroupFormFilter();   /*initialisation de l'objet filters */
    $this->values = $this->getFilters();

   $this->id_user = $this->getUser()->getGuardUser()->getId();

   $c = new Criteria();
   $c->add(PagePeer::ID_USER,$this->id_user);
   $this->css = PagePeer::doSelect($c);
   // $this->sf_guard_group_list = sfGuardGroupPeer::doSelect(new Criteria());
  }

  public function executeNew(sfWebRequest $request)
  {

    $this->form = new sfGuardUserGroupForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
  //  $this->forward404Unless($request->isMethod('post'));
    $this->id_group = $request->getParameter('id_groupe');
    $this->id_user = $this->getUser()->getGuardUser()->getId();

    $groupe = new sfGuardUserGroup();
    $groupe->setUserId($this->id_user);
    $groupe->setGroupId($this->id_group);

    $this->form = new sfGuardUserGroupForm($groupe);

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($sf_guard_group = sfGuardGroupPeer::retrieveByPk($request->getParameter('id')), sprintf('Object sf_guard_group does not exist (%s).', $request->getParameter('id')));
    $this->form = new sfGuardGroupForm($sf_guard_group);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post') || $request->isMethod('put'));
    $this->forward404Unless($sf_guard_group = sfGuardGroupPeer::retrieveByPk($request->getParameter('id')), sprintf('Object sf_guard_group does not exist (%s).', $request->getParameter('id')));
    $this->form = new sfGuardGroupForm($sf_guard_group);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($sf_guard_group = sfGuardGroupPeer::retrieveByPk($request->getParameter('id')), sprintf('Object sf_guard_group does not exist (%s).', $request->getParameter('id')));
    $sf_guard_group->delete();

    $this->redirect('recherche/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $sf_guard_group = $form->save();

      $this->redirect('recherche/index');
    }
  }

  protected function getFilters()
  {
    return $this->getUser()->getAttribute('recherche.filters', array());
  }

  protected function setFilters(array $filters)
  {
    return $this->getUser()->setAttribute('recherche.filters', $filters);
  }

  protected function buildCriteria()
  {
    if (is_null($this->filters))
    {
      $this->filters =  new sfGuardGroupFormFilter($this->getFilters());
    }

    $criteria = $this->filters->buildCriteria($this->getFilters());

    return $criteria;
  }

  public function executeFilter(sfWebRequest $request)
  {
    $this->filters = new sfGuardGroupFormFilter($this->getFilters());

    $this->filters->bind($request->getParameter($this->filters->getName()));
    if ($this->filters->isValid())
    {
      $this->setFilters($this->filters->getValues());
    }

     $this->redirect('recherche/recherche');
  }

  public function executeRecherche(sfWebRequest $request)
  {
    $this->user = $this->getUser()->getGuardUser()->getId();

    $c = $this->buildCriteria();

    $pager = new sfPropelPager('sfGuardGroup',10);
    $pager->setCriteria($c);
    $pager->setPage($this->getRequestParameter('page', 1));
    $pager->init();
    $this->pager = $pager;

   $c = new Criteria();
   $c->add(PagePeer::ID_USER,$this->user);
   $this->css = PagePeer::doSelect($c);
  }

}
